package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private DishService dishService;
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> add(@RequestBody SetmealDto setmealDto) {
        boolean save = setMealService.saveMeal(setmealDto);
        return save ? R.success("添加套餐成功") : R.error("添加套餐失败");
    }




    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> PageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setMealService.page(PageInfo, queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(PageInfo, setmealDtoPage, "records");
        List<Setmeal> records = PageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }



    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, Long ids[]) {
        List<Long> longList = Arrays.asList(ids);
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId, longList);
        updateWrapper.set(Setmeal::getStatus, status);
        boolean update = setMealService.update(updateWrapper);
        return update ? R.success("更新套餐状态成功") : R.error("更新套餐状态失败");
    }


    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteByIds(@RequestParam("ids") List<Long> ids) {
        boolean b = setMealService.deleteByIds(ids);
        return b ? R.success("删除成功") : R.error("删除失败");
    }


    @GetMapping("/{id}")
    public R<Setmeal> update(@PathVariable Long id) {
        SetmealDto setmealDto = setMealService.Update(id);
        return R.success(setmealDto);
    }


    @PutMapping
    public R<String> Update(@RequestBody SetmealDto setmealDto) {
        boolean update = setMealService.Update(setmealDto);
        return update ? R.success("更新套餐成功") : R.error("更新套餐失败");
    }


    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setMealService.list(queryWrapper);
        return R.success(list);
    }


    @GetMapping("/dish/{id}")
    public R Get(@PathVariable Long id) {
        Setmeal setmeal = setMealService.getById(id);
        return R.success(setmeal);
    }
}
